define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/stats/models'),
        utils = require('js/utils/tools'),
        chart = require('js/utils/chart'),
        app = Backbone.history;
    require('datetimepicker');

    var UninstallListView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
            'click #exportData': 'exportData',
        },
        initialize: function (options) {
            this.options = options;
            this.collection = new models.UninstallCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        exportData: function (e) {
            e.preventDefault();
            if (this.collection.total == 0) {
                return;
            }
            if (this.collection.total > 1000) {
                notify.warning('警告', '数据量较大，请耐心等待');
            }
            utils.exportData(this.collection.url, this.options);
            return false;
        },
        doSearch: function () {
            var options = {},
                searched = {};

            searched.remove_at_lower = $("#searchRemoveAtLower").val();
            searched.remove_at_upper = $('#searchRemoveAtUpper').val();
            if (searched.remove_at_lower || searched.remove_at_upper) {
                options.remove_at = {};
                if (searched.remove_at_lower) options.remove_at.$gte = searched.remove_at_lower;
                if (searched.remove_at_upper) options.remove_at.$lt = searched.remove_at_upper;
            }
            utils.saveSearched('stats/uninstall', searched);
            app.navigate(utils.composeQueryString('#stats/uninstall/', options), {
                trigger: true
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                overview: this.collection.overview,
                searched: utils.getSearched('stats/uninstall')
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'id',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#stats/uninstall/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            $('.date-input').datetimepicker({
                format: 'YYYY-MM-DD'
            });
            if (this.collection.overview.questionare) {
                var options = chart.getHighChartPieOptions(),
                    series = options.series[0].data;
                _.each(this.collection.overview.questionare.dist, function (v, k) {
                    if (v > 0) series.push({
                        name: "选项" + k,
                        y: v
                    });
                });
                $('#reasonDist').highcharts(options);
            }
            utils.getPaginator(that.options, this.collection.total, '#stats/uninstall/');
            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "stats/UninstallList.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var view = new UninstallListView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });

});